using System;
using QntPlatform.Strategy.Utility;

namespace QntPlatform
{
    public class FixedProfitAction : SActionBase
    {
        //止盈价
        public decimal ProfitStopPrice { get; private set; }
        public bool IsLong { get; private set; }

        public FixedProfitAction(bool isLong, decimal profitStopPrice, ILog log = null)
        {
            IsLong = isLong;
            ProfitStopPrice = profitStopPrice;
        }
        ILog log;

        public bool? Execute(Ticker tck)
        {
            if (IsDisposable)
                return null;
            if (CalcFun.CalcUnitProfit(tck.Buy, ProfitStopPrice, IsLong) >= 0)
            {
                log?.Debug("固定止盈计算返回true", new { ProfitStopPrice, tck.Buy }); 
                return TrueReturn();
            }
            return FalseReturn();
        }

    }
    public abstract class SActionBase : IDisposable
    {
        public bool IsDisposable { get; private set; }
        public bool IsOnlyExToDisposable { get; private set; } = false;
        public void Dispose()
        {
            IsDisposable = true;
        }
        public Action<bool> AfterFun { get; set; }
        public bool TrueReturn()
        {
            AfterFun?.Invoke(true);
            if (IsOnlyExToDisposable)
                Dispose();
            return true;
        }
        public bool FalseReturn()
        {
            AfterFun?.Invoke(false);
            return false;
        }
    }
}
